package com.truyen.utils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;


public class PagingTagA extends TagSupport {
	static Logger logger = Logger.getLogger(PagingTagA.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -3479891350802464345L;
	private PagingInfo pagingInf;
	private String searchUrl;
	private String curPageParam;
	private String searchValueParam;

	@Override
	public int doStartTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			String strTrangDau = new String("Đầu");
			String strTrangTruoc = new String("&nbsp;");
			String strTrangSau = new String("&nbsp;");
			String strTrangCuoi = new String("Cuối");
			int totalPage = pagingInf.getTotalPage();
			if(totalPage > 1) {
//				String searchValue = pagingInf.getSearchValue();
				int curPage = pagingInf.getCurrentPage();
				try {
					if(curPage != 1) {
						out.write("<a href=\""+ searchUrl + curPageParam + "1" 
								+ searchValueParam
								+ "\" title=\""+strTrangDau+"\" class=\"firstPage corner\">"+strTrangDau+"</a>");
					}
					
					if(curPage <= totalPage && curPage != 1) {
						out.write("<a href=\""+ searchUrl + curPageParam + (curPage - 1)
								+ searchValueParam
								+ "\" title=\""+strTrangTruoc+"\" class=\"prevPage corner\">"+strTrangTruoc+"</a>");
					}
					
					for(int i = pagingInf.getBegin(); i <= pagingInf.getEnd(); i++) {
						if(i == curPage) {
							out.write("<span class=\"pageActive corner\">"+ i + "</span>");
						} else {
							out.write("<a href=\""+ searchUrl + curPageParam + i
									+ searchValueParam
									+ "\" title=\"Trang "+ i
									+ "\" class=\"previous corner\">"+ i + "</a>");
						}
					}
					
					if(curPage < totalPage) {
						out.write("<a href=\""+ searchUrl + curPageParam + (curPage + 1) 
								+ searchValueParam
								+ "\" title=\""+strTrangSau+"\" class=\"nextPage corner\">"+strTrangSau+"</a>");
					}
					
					if(curPage != totalPage) {
						out.write("<a href=\""+ searchUrl + curPageParam + totalPage
								+ searchValueParam
								+ "\" title=\""+strTrangCuoi+"\" class=\"lastPage corner\">"+strTrangCuoi+"</a>");
					}				
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e1) {
			logger.error(e1.getMessage(), e1);
		}		

		return SKIP_BODY;
	}

	/**
	 * @param pagingInf
	 *            the pagingInf to set
	 */
	public void setPagingInf(PagingInfo pagingInf) {
		this.pagingInf = pagingInf;
	}

	/**
	 * @return the pagingInf
	 */
	public PagingInfo getPagingInf() {
		return pagingInf;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setSearchUrl(String url) {
		this.searchUrl = url;
	}

	/**
	 * @return the url
	 */
	public String getSearchUrl() {
		return searchUrl;
	}

	/**
	 * @param curPageParam
	 *            the curPageParam to set
	 */
	public void setCurPageParam(String curPageParam) {
		this.curPageParam = curPageParam;
	}

	/**
	 * @return the curPageParam
	 */
	public String getCurPageParam() {
		return curPageParam;
	}

	/**
	 * @param searchValueParam
	 *            the searchValueParam to set
	 */
	public void setSearchValueParam(String searchValueParam) {
		this.searchValueParam = searchValueParam;
	}

	/**
	 * @return the searchValueParam
	 */
	public String getSearchValueParam() {
		return searchValueParam;
	}
}
